package com.cyandpos.pad.sale.model;

import android.text.TextUtils;
import android.util.Log;


import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.DjdData;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.posdata.Member;
import com.cyandpos.pad.posdata.OrderNoData;
import com.cyandpos.pad.posdata.Person;
import com.cyandpos.pad.posdata.PreparePayData;
import com.cyandpos.pad.posdata.PromationoodInfo;
import com.cyandpos.pad.posdata.QueryGoodParam;
import com.cyandpos.pad.posdata.QueryKDticket;
import com.cyandpos.pad.posdata.QueryMemberReq;
import com.cyandpos.pad.posdata.QueryOrderReq;
import com.cyandpos.pad.posdata.SubTicketItem;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.utils.MoneyUtils;
import com.cyandpos.pad.utils.log.LogUtils;
import com.google.gson.JsonObject;

import java.math.BigDecimal;
import java.util.List;

public class SaleModelImpl implements ISaleModel{
    /**
     * 创建订单
     * @param orderNo
     * @param listener
     */
    @Override
    public void createOrderNo(String orderNo, final onCreateOrderNoListener listener) {
        PosHttpManager.CreateSaleId(orderNo,"","", new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<OrderNoData> baseResponse = BaseResponse.fromJson(response.toString(),OrderNoData.class);
                if(baseResponse.getCode().equals("0")){
                    listener.onCreateOrderNoSuccess(baseResponse.getData().getOrderNo());
                }else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onCreateOrderNoFailure(baseResponse.getMsg());
                    }else {
                        listener.onCreateOrderNoFailure(baseResponse.getTips());
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                listener.onCreateOrderNoFailure(msg);
            }
        });

    }

    /**
     * 查询营业员
     * @param personCode
     * @param listener
     */
    @Override
    public void queryPerson(String personCode,String orderNo, final OnQueryPersonListener listener) {
        PosHttpManager.QueryPersonExecute(personCode, orderNo,new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                Log.i("TAG----营业员信息",response.toString());
                BaseResponse<Person> baseResponse = BaseResponse.fromJson(response.toString(),Person.class);
                if(baseResponse.getCode().equals("0")){
                    listener.onQueryPersonSuccess(baseResponse.getData());
                }else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onQueryPersonFailure(baseResponse.getMsg());
                    }else {
                        listener.onQueryPersonFailure(baseResponse.getTips());
                    }
                }
            }
            @Override
            public void onFailure(String msg) {
                listener.onQueryPersonFailure(msg);
            }
        });
    }

    /**
     * 商品查询
     * @param content
     * @param ticketInfo
     * @param listener
     */
    @Override
    public void queryGoods(QueryGoodParam content, TicketInfo ticketInfo, int subInx, final OnQueryGoodsInfoListener listener) {
        PosHttpManager.QueryGoodsInfoExecute(content, ticketInfo,subInx, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                Log.i("TAG----商品查询",response.toString());
                BaseResponse<Goods> baseResponse = BaseResponse.fromJson(response.toString(),Goods.class);
                if(baseResponse.getCode().equals("0")){
                    listener.onQueryGoodsInfoSuccess(baseResponse.getData());
                }else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onQueryGoodsInfoFailure(baseResponse.getMsg());
                    }else {
                        listener.onQueryGoodsInfoFailure(baseResponse.getTips());
                    }
                }
            }
            @Override
            public void onFailure(String msg) {
                listener.onQueryGoodsInfoFailure(msg);
            }
        });

    }

    /**
     * 前台折扣
     * @param disType  折扣类型
     * @param money  折扣金额(折扣率)
     * @param goods   单品
     * @param orderNo  订单号
     * @param isSingle  是否单品折扣
     * @param listener
     */
    @Override
    public void frontDiscount(String disType, String money, Goods goods, String orderNo, final boolean isSingle, final OnFrontDiscountListener listener) {
        PosHttpManager.FrontDiscount(disType, money, goods, orderNo, isSingle, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<TicketInfo> baseResponse = BaseResponse.fromJson(response.toString(),TicketInfo.class);
                if(baseResponse.getCode().equals("0")){
                    if(baseResponse.getData().getSubTickets()!=null&&baseResponse.getData().getSubTickets().get(0).getGoodsList().size()>0){
                        listener.onFrontDiscountSuccess(baseResponse.getData().getSubTickets().get(0).getGoodsList(),isSingle);
                    }else {
                        if (TextUtils.isEmpty(baseResponse.getTips())){
                            listener.onFrontDiscountFailure(baseResponse.getMsg());
                        }else {
                            listener.onFrontDiscountFailure(baseResponse.getMsg());
                        }
                    }
                }else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onFrontDiscountFailure(baseResponse.getMsg());
                    }else {
                        listener.onFrontDiscountFailure(baseResponse.getMsg());
                    }
                }
            }
            @Override
            public void onFailure(String msg) {
                listener.onFrontDiscountFailure(msg);
            }
        });
    }

    /**
     * 会员查询
     * @param memberReq  请求需要的数据
     * @param listener
     */
    @Override
    public void queryMember(final QueryMemberReq memberReq, final OnQueryMemberListener listener) {
        PosHttpManager.QueryMemberExecute(memberReq, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<Member> baseResponse = BaseResponse.fromJson(response.toString(),Member.class);
                if(baseResponse.getCode().equals("0")){
                    listener.OnQueryMemberSuccess(baseResponse.getData());
                }
                else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        if(baseResponse.getMsg().contains("不存在")){
                            if("3".equals(memberReq.getCondType())) {
                                listener.OnRegisterMemberFailure(memberReq, baseResponse.getMsg());
                            }else{
                                listener.OnQueryMemberFailure(baseResponse.getMsg());
                            }
                        }else {
                            listener.OnQueryMemberFailure(baseResponse.getMsg());
                        }
                    }else {
                        if(baseResponse.getTips().contains("不存在")){
                            if("3".equals(memberReq.getCondType())) {
                                listener.OnRegisterMemberFailure(memberReq, baseResponse.getTips());
                            }else{
                                listener.OnQueryMemberFailure(baseResponse.getTips());
                            }
                        }else {
                            listener.OnQueryMemberFailure(baseResponse.getTips());
                        }
                    }
                }

            }

            @Override
            public void onFailure(String msg) {
                listener.OnQueryMemberFailure(msg);

            }
        });

    }

    @Override
    public void cancelOrder(final String orderNo, final onCancelOrderListener listener) {
        PosHttpManager.cancelOrder(orderNo, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<OrderNoData> baseResponse = BaseResponse.fromJson(response.toString(),OrderNoData.class);
                if(baseResponse.getCode().equals("0")){
                    listener.onCancelOrderSuccess("");
                }else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onCancelOrderFailure(baseResponse.getMsg());
                    }else {
                        listener.onCancelOrderFailure(baseResponse.getTips());
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                listener.onCancelOrderFailure(msg);

            }
        });

    }

    @Override   // 查询开单商品
    public void queryOrder(QueryOrderReq req, final onQueryOrderlListener listener) {
        PosHttpManager.QueryOrderInfoEexcute(req,new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                try {
                    Log.i("TAG-----查询开单商品",response.toString());

                    BaseResponse<QueryKDticket> baseResponse = BaseResponse.fromJson(response.toString(), QueryKDticket.class);
                    if (baseResponse.getCode().equals("0")){
                        listener.onQueryOrderSuccess(baseResponse.getData().getOrderList().get(0).getSubTickets());
                    }else {
                        if (TextUtils.isEmpty(baseResponse.getTips())){
                            onFailure(baseResponse.getMsg());
                        }else {
                            onFailure(baseResponse.getTips());
                        }
                    }
                }catch (Exception e){
                    onFailure("数据解析失败"+e.toString());
                }
            }
            @Override
            public void onFailure(String msg) {
                listener.onQueryOrderFailure(msg);

            }
        });

    }

    /**
     * 准备付款
     * @param ticketInfo  小票信息
     * @param listener
     */
    @Override
    public void onPreparePay(final TicketInfo ticketInfo, final boolean isPromotion, List<PromationoodInfo> exchangeGoodBillInfoList, List<PromationoodInfo> giftGoodsBillList, int promotionGoodsSelectSign, final onPreparePayListener listener) {
        PosHttpManager.PreparePay(ticketInfo,isPromotion,exchangeGoodBillInfoList,giftGoodsBillList,promotionGoodsSelectSign, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                Log.i("TAG----准备付款",response.toString());
                BaseResponse<PreparePayData> baseResponse = BaseResponse.fromJson(response.toString(),PreparePayData.class);
                if(baseResponse.getCode().equals("0")){
                    ticketInfo.setDecMoney(MoneyUtils.getMabMoneyString(baseResponse.getData().getDecMoney()));
                    for(SubTicketItem sub :ticketInfo.getSubTickets()){
                        sub.getGoodsList().clear();
                        for(Goods g: baseResponse.getData().getGoodsList()){
                            if(g.getTicktInx().equals(sub.getSubInx()) ){
                                Goods backGood = g.goodClone();
                                sub.getGoodsList().add(backGood);
                            }
                        }
                    }
                    ticketInfo.setTotalAmount(MoneyUtils.getMabMoneyString(baseResponse.getData().getTotalAmount()));
                    ticketInfo.setTotalCount(baseResponse.getData().getTotalCount());
                    ticketInfo.setDecPreferentialMoney(MoneyUtils.getMabMoneyString(baseResponse.getData().getDecPreferentialMoney()));
                    ticketInfo.setTotalDiscount(MoneyUtils.getMabMoneyString(baseResponse.getData().getTotalDiscount()));
                    ticketInfo.setServerBillId(baseResponse.getData().getServerBillId());
                    ticketInfo.setJlbh(baseResponse.getData().getJlbh());
                    listener.onPreparePaySuccess(ticketInfo,isPromotion,baseResponse.getData().getExchangeGoodsBillInfoList(),baseResponse.getData().getGiftGoodsBillList());
                }else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onPreparePayFailure(baseResponse.getMsg());
                    }else {
                        listener.onPreparePayFailure(baseResponse.getTips());
                    }
                        
                }

            }

            @Override
            public void onFailure(String msg) {
                listener.onPreparePayFailure(msg);

            }
        });

    }


    /**
     * 封装错误信息
     * @param resCode
     * @param Msg
     * @return
     */
    private String getErrorMsg(String resCode, String Msg) {
        return "返回码:" + resCode + ",错误信息:" + Msg;
    }
}
